public class Leetcode74 {
    public static void main(String[] args) {
//        System.out.println(searchMatrix(new int[][]{
//                {1, 3, 5, 7},
//                {10, 11, 16, 20},
//                {23, 30, 34, 60},
//        }, 3));

        System.out.println(searchMatrix(new int[][]{
                {1, 1},
        }, 2));
    }

    public static boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;

        int left = 0;
        int right = m * n - 1;
        while (left <= right) {
            int middle = (right - left) / 2 + left;
            int x = middle / n;
            int y = middle % n;
            if (matrix[x][y] == target) {
                return true;
            }

            if (matrix[x][y] > target) {
                right = middle - 1;
            } else {
                left = middle + 1;
            }
        }

        return false;
    }
}
